/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.io.IOException;
import java.math.BigInteger;
import java.net.Socket;
import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import sql.AConnection;

/**
 *
 * @author Administrator
 */
public class Action extends AConnection{

    public static String MD5(String in) {
        String out = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(in.getBytes());
            BigInteger hash = new BigInteger(1, md5.digest());
            out = hash.toString(16);

        } catch (Exception e) {
        }
        return out;
    }

    public static String converDatetoMysql(String date) {
        char[] list = date.toCharArray();
        if (list.length == 10) {
            try {
                String day = "" + list[0] + list[1];
                String month = "" + list[3] + list[4];
                String year = "" + list[6] + list[7] + list[8] + list[9];
                Integer.parseInt(day + month + year);
                return year + "-" + month + "-" + day;
            } catch (Exception e) {
                return null;
            }
        } else {
            return null;
        }

    }

    public static String converDatetoSite(String date) {
        char[] list = date.toCharArray();
        if (list.length == 10) {
            try {
                String year = "" + list[0] + list[1] + list[2] + list[3];
                String month = "" + list[5] + list[6];
                 String day = "" + list[8] + list[9];
                Integer.parseInt(day + month + year);
                return day + "-" + month + "-" + year;
            } catch (Exception e) {
                return null;
            }
        } else {
            return null;
        }

    }
     public static String converToHTML(String string) {
         if(string==null)return "";
        StringBuilder sb = new StringBuilder(string.length());
        boolean lastWasBlankChar = false;
        int len = string.length();
        char c;
        for (int i = 0; i < len; i++) {
            c = string.charAt(i);
            if (c == ' ') {
                if (lastWasBlankChar) {
                    lastWasBlankChar = false;
                    sb.append("&nbsp;");
                } else {
                    lastWasBlankChar = true;
                    sb.append(' ');
                }
            } else {
                lastWasBlankChar = false;
                if (c == '"') {
                    sb.append("&quot;");
                } else if (c == '&') {
                    sb.append("&amp;");
                } else if (c == '<') {
                    sb.append("&lt;");
                } else if (c == '>') {
                    sb.append("&gt;");
                } else if (c == '\n') {
                    sb.append("&lt;br/&gt;");
                } else {
                    int ci = 0xffff & c;
                    if (ci < 160) {
                        sb.append(c);
                    } else {
                        sb.append("&#");
                        sb.append(new Integer(ci).toString());
                        sb.append(';');
                    }
                }
            }
        }
        return sb.toString();
    }

     //----------------------------
     /**
      * phần dành cho những hàm có dung đên cơ sở dữ liệu.
      * @param args
      */
     public static ResultSet select(String query){
         ResultSet rs=null;
        try {
            Connection connection = getConnection();
            Statement s = connection.createStatement();
            rs=s.executeQuery(query);
            return rs;
        } catch (SQLException ex) {
            return  rs;
        }

     }

     //----------------------------


    public static boolean checkemail(String email){

        if (email.equals("")) {
            return false;
        }

        Pattern pattern = Pattern.compile("\\w+@\\w+(\\W.\\w+)*");
        Matcher m = pattern.matcher(email);
        if (!m.matches()) {
            return false;
        }

        ResultSet rs = Action.select("SELECT Email FROM users where Email= '"+email+"'");
        try {
            if (rs.next()) {
                return false;
            }
        } catch (SQLException ex) {
           
        }

        String host = "";
        StringTokenizer token = new StringTokenizer(email, "@");
        while (token.hasMoreTokens()) {
            host = token.nextToken();
        }

        if (host.equals("gmail.com") || host.equals("yahoo.com") || host.equals("zing.vn") || host.equals("hotmail.com") || host.equals("st.hcmuaf.edu.vn")) {
            return true;
        }

        try {
            Socket socket = new Socket(host, 80);
            return true;
        } catch (IOException iOException) {
            return false;
        }

        
    }
    public static boolean checkUsername(String username){
        try {
            String query = "Select * from users where username =?";
            Connection connection = getConnection();
            PreparedStatement ps = connection.prepareStatement(query);
            ps.setString(1, username);
            ResultSet rs=ps.executeQuery();
            return !rs.next();
        } catch (SQLException ex) {
            return false;
        }

    }
}
